package cn.micai.base.io;

import java.io.*;

/**
 * 描述：如何在Java中复制文件
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/20 15:11
 */
public class CopyFileExample {

    public static void main(String [] args) {

        // 输入流
        InputStream inputStream = null;
        // 输出流
        OutputStream outputStream = null;

        try {
            File afile = new File("D://newFile.txt");
            File bfile = new File("D://newFile2.txt");

            inputStream = new FileInputStream(afile);
            outputStream = new FileOutputStream(bfile);

            byte[] buffer = new byte[1024];
            int length;
            // copy the file content in bytes
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
